/**
 * 创建人:  @author huangpeng    
 * 创建时间:  2023年11月05日 18:31
 * 项目名称:  lemon-blog-cloud
 * 文件名称:  BiEmailController
 * 文件描述:  @Description: 邮件controller
 * <p>
 * All rights Reserved, Designed By huangpeng 
 * @Copyright: 2021-2023
 *
 */
package com.lemon.message.controller;

import com.lemon.common.core.utils.poi.ExcelUtil;
import com.lemon.common.core.web.controller.BaseController;
import com.lemon.common.core.web.domain.AjaxResult;
import com.lemon.common.core.web.page.TableDataInfo;
import com.lemon.common.log.annotation.Log;
import com.lemon.common.log.enums.BusinessType;
import com.lemon.common.security.annotation.RequiresPermissions;
import com.lemon.message.domain.BiEmail;
import com.lemon.message.service.BiEmailService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutionException;

/**
 * 包名称：com.lemon.message.controller
 * 类名称：BiEmailController
 * 类描述：邮件controller
 * 创建人：@author huangpeng
 * 创建时间：2023年11月05日 18:31
 */
@Api("邮件记录")
@RestController
@RequestMapping("/email")
public class BiEmailController extends BaseController {

    @Autowired
    private BiEmailService biEmailService;

    @PostMapping("/send")
    public AjaxResult sendEmail(@RequestBody BiEmail model) {
        try {
            boolean flag = biEmailService.sendEmail(model).get();
            return toAjax(flag);
        } catch (InterruptedException | ExecutionException e) {
            return AjaxResult.error("邮件发送失败");
        }
    }

    /**
     * 查询邮件记录列表
     * @Title: list
     * @Description: 查询邮件记录列表
     * @param biEmail 邮件记录
     * @return: TableDataInfo
     * @throws:
     * @author: lemon
     * @Date: 2023-11-09
     */
    @RequiresPermissions("message:email:list")
    @GetMapping("/list")
    public TableDataInfo list(BiEmail biEmail) {
        startPage();
        List<BiEmail> list = biEmailService.selectBiEmailList(biEmail);
        return getDataTable(list);
    }

    /**
     * 导出邮件记录列表
     * @Title: export
     * @Description: 导出邮件记录列表
     * @param response 客户端响应对象
     * @param biEmail 邮件记录
     * @return:
     * @throws:
     * @author: lemon
     * @Date: 2023-11-09
     */
    @RequiresPermissions("message:email:export")
    @Log(title = "邮件记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BiEmail biEmail) {
        List<BiEmail> list = biEmailService.selectBiEmailList(biEmail);
        ExcelUtil<BiEmail> util = new ExcelUtil<BiEmail>(BiEmail.class);
        util.exportExcel(response, list, "邮件记录数据");
    }

    /**
     * 获取邮件记录详细信息
     * @Title: getInfo
     * @Description: 获取邮件记录详细信息
     * @param id 邮件记录主键
     * @return: AjaxResult
     * @throws:
     * @author: lemon
     * @Date: 2023-11-09
     */
    @RequiresPermissions("message:email:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(biEmailService.selectByPrimaryKey(id));
    }

    /**
     * 新增邮件记录
     * @Title: add
     * @Description: 新增邮件记录
     * @param biEmail 邮件记录
     * @return: AjaxResult
     * @throws:
     * @author: lemon
     * @Date: 2023-11-09
     */
    @RequiresPermissions("message:email:add")
    @Log(title = "邮件记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BiEmail biEmail) {
        return toAjax(biEmailService.insert(biEmail));
    }

    /**
     * 修改邮件记录
     * @Title: edit
     * @Description: 修改邮件记录
     * @param biEmail 邮件记录
     * @return: AjaxResult
     * @throws:
     * @author: lemon
     * @Date: 2023-11-09
     */
    @RequiresPermissions("message:email:edit")
    @Log(title = "邮件记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BiEmail biEmail) {
        return toAjax(biEmailService.update(biEmail));
    }

    /**
     * 删除邮件记录
     * @Title: remove
     * @Description: 删除邮件记录
     * @param ids 需要删除的邮件记录主键集合
     * @return: AjaxResult
     * @throws:
     * @author: lemon
     * @Date: 2023-11-09
     */
    @RequiresPermissions("message:email:remove")
    @Log(title = "邮件记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(biEmailService.deleteByBatch(ids));
    }

    /**
     * 发送验证码邮件
     * @Title: sendCaptcha
     * @Description: 发送验证码邮件
     * @param userMap 收件人信息
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/11/18 17:36
     */
    @PostMapping("/sendCaptcha")
    public AjaxResult sendCaptcha(@RequestBody Map<String, Object> userMap) {
        String uuid = biEmailService.sendCaptcha(userMap);
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("uuid", uuid);
        return ajaxResult;
    }

    /**
     * 发送注册验证码
     * @Title: sendRegisterCaptcha
     * @Description: 发送注册验证码
     * @param userMap 收件人信息
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/11/26 1:33
     */
    @PostMapping("/sendRegisterCaptcha")
    public AjaxResult sendRegisterCaptcha(@RequestBody Map<String, Object> userMap) {
        String uuid = biEmailService.sendRegisterCaptcha(userMap);
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("uuid", uuid);
        return ajaxResult;
    }

    /**
     * 发送重置用户密码邮件
     * @Title: sendRestPasswordCaptcha
     * @Description: 发送重置用户密码邮件
     * @param userMap 收件人信息
     * @return: AjaxResult
     * @throws:
     * @author: huangpeng
     * @Date: 2023/11/26 19:02
     */
    @PostMapping("/sendRestPasswordCaptcha")
    public AjaxResult sendRestPasswordCaptcha(@RequestBody Map<String, Object> userMap) {
        String uuid = biEmailService.sendRestPasswordCaptcha(userMap);
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("uuid", uuid);
        return ajaxResult;
    }

}
